package com.xc.content.controller;

import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.xc.content.bo.CourseBO;
import com.xc.content.bo.CourseBaseBO;
import com.xc.content.constant.ExceptionCode;
import com.xc.content.convert.CourseBaseConverter;
import com.xc.content.convert.CourseBaseServiceConverter;
import com.xc.content.dto.CourseBaseDTO;
import com.xc.content.dto.CourseDTO;
import com.xc.content.entity.CourseBase;
import com.xc.content.service.CourseBaseService;
import com.xc.content.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: hyacinth
 * @CreateTime: 2024-12-25
 * @Version: 1.0
 * 课程信息编辑接口
 */
@RestController
@RequestMapping("/content/course")
@Api(value = "课程信息编辑接口", tags = "课程信息编辑接口")
@Slf4j
public class CourseBaseInfoController {
    @Resource
    private CourseBaseService courseBaseService;

    @ApiOperation(value = "新增课程接口")
    @PostMapping("/courseBaseSave")
    public ResponseResult<?> courseBaseSave(@RequestBody CourseDTO courseDTO) {
        Preconditions.checkNotNull(courseDTO.getCompanyId(), "机构id不能为空");
        Preconditions.checkNotNull(courseDTO.getCompanyName(), "机构名称不能为空");
        Preconditions.checkNotNull(courseDTO.getName(), "课程名称不能为空");
        Preconditions.checkNotNull(courseDTO.getUsers(), "适用人群不能为空");
        Preconditions.checkNotNull(courseDTO.getTags(), "课程标签不能为空");
        Preconditions.checkNotNull(courseDTO.getMt(), "大分类不能为空");
        Preconditions.checkNotNull(courseDTO.getSt(), "小分类不能为空");
        Preconditions.checkNotNull(courseDTO.getGrade(), "课程等级不能为空");
        Preconditions.checkNotNull(courseDTO.getCreatePeople(), "创建人不能为空");
        Preconditions.checkNotNull(courseDTO.getDescription(), "课程介绍不能为空");
        Preconditions.checkNotNull(courseDTO.getTeachMode(), "课程模式不能为空");
        Preconditions.checkNotNull(courseDTO.getCharge(), "收费规则不能为空");
        Preconditions.checkNotNull(courseDTO.getValidDays(), "有效期(天)不能为空");

        CourseBO courseBO = CourseBaseConverter.INSTANCE.courseDTOToBO(courseDTO);
        Boolean isSave = courseBaseService.courseBaseSave(courseBO);
        return ResponseResult.success("课程新增成功",isSave);
    }

    @ApiOperation(value = "查询所有课程接口")
    @GetMapping("/list")
    public ResponseResult<?> list() {
        List<CourseBase> allList = courseBaseService.findAll();
        List<CourseBaseBO> courseBaseBOS = CourseBaseServiceConverter.INSTANCE.courseBaseListToBOList(allList);
        List<CourseBaseDTO> courseBaseDTOList = CourseBaseConverter.INSTANCE.courseBaseListToDTOList(courseBaseBOS);
        return ResponseResult.success("查询成功", courseBaseDTOList);
    }

    @ApiOperation(value = "根据条件查询")
    @PostMapping("/ConditionalQuery")
    public ResponseResult<?> ConditionalQuery(@RequestBody CourseBaseDTO courseBaseDTO) {
        CourseBaseBO courseBaseBO = CourseBaseConverter.INSTANCE.courseBaseDTOToBO(courseBaseDTO);
        List<CourseBase> courseBase = courseBaseService.ConditionalQuery(courseBaseBO);
        return ResponseResult.success("查询成功", courseBase);
    }

    @ApiOperation(value = "分页条件查询课程信息")
    @PostMapping("/findPageWithQuery")
    public ResponseResult<?> findPageWith(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize,
                                          @RequestBody(required = false) CourseBaseDTO courseBaseDTO) {
        CourseBaseBO courseBaseBO = CourseBaseConverter.INSTANCE.courseBaseDTOToBO(courseBaseDTO);
        PageInfo<CourseBase> page = courseBaseService.findPageWithQuery(pageNum, pageSize, courseBaseBO);
        return ResponseResult.success("查询成功", page);
    }

    @ApiOperation(value = "修改课程接口")
    @PostMapping("/updateCourseBase")
    public ResponseResult<?> updateCourseBase(@RequestBody CourseBaseDTO courseBaseDTO) {
        Preconditions.checkNotNull(courseBaseDTO.getId(), "id不能为空");
        Preconditions.checkNotNull(courseBaseDTO.getChangePeople(), "更新人不能为空");

        CourseBaseBO courseBaseBO = CourseBaseConverter.INSTANCE.courseBaseDTOToBO(courseBaseDTO);
        Boolean isSave = courseBaseService.updateCourseBase(courseBaseBO);
        if (isSave) {
            return ResponseResult.success("课程修改成功");
        } else {
            return ResponseResult.error(ExceptionCode.FIVE_HUNDRED_CODE, "课程修改失败");
        }
    }

    @ApiOperation(value = "删除课程接口")
    @PostMapping("/deletedCourseBase")
    public ResponseResult<?> deletedCourseBase(@RequestBody CourseBaseDTO courseBaseDTO) {
        Preconditions.checkNotNull(courseBaseDTO.getId(), "id不能为空");

        CourseBaseBO courseBaseBO = CourseBaseConverter.INSTANCE.courseBaseDTOToBO(courseBaseDTO);
        Boolean isSave = courseBaseService.deletedCourseBase(courseBaseBO);
        if (isSave) {
            return ResponseResult.success("课程删除成功");
        } else {
            return ResponseResult.error(ExceptionCode.FIVE_HUNDRED_CODE, "课程删除失败");
        }
    }


}
